package com.ys.controller.api.sys;

import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.ys.entity.sys.YsGridButton;
import com.ys.entity.sys.YsMenu;
import com.ys.service.sys.CommonApiService;
import com.ys.service.sys.YsGridButtonService;
import com.ys.vo.LayuiTableVoJA;
import com.ys.vo.R;

@Controller
@RequestMapping("/api/inter/ysGridButton")
public class ApiInterYsGridButtonController {
	
//	private static final Logger logger = Logger.getLogger(ApiAdminLoginController.class);
	
	@Autowired
	private CommonApiService<YsGridButton> commonApiService;
	@Autowired
	private YsGridButtonService ysGridButtonService;

	@RequiresPermissions("ysGridButton:view")
	@RequestMapping("/search")
    @ResponseBody
	public LayuiTableVoJA search(Model model, HttpServletRequest request, HttpServletResponse response,
			@ModelAttribute YsGridButton cond
			){
		
		cond.addLeftJoin("YS_Grid B on B.Id = A.GridId")
			.addField("B.Name gridName");
		
		cond.setOrderby("A.GridId asc, A.SortNo asc");
		
		return commonApiService.searchLay(cond);
	}
	
	/**
	 * 新增
	 */
	@PostMapping("/add")
    @ResponseBody
	public R add(Long gridId,
			Integer styleType,
			String name,
			String event,
			String icon,
			String nameShiroAuth,
			Integer sortNo,
			String remark){
		
		return ysGridButtonService.add(gridId, styleType, name, event, icon, nameShiroAuth, sortNo, remark);
	}
	
	/**
	 * 新增单个
	 */
	@PostMapping("/addSingle")
    @ResponseBody
	public R addSingle(Long menuId){
		
		if (menuId == null) {
			return R.error("子列表所属菜单ID不能为空");
		}
		YsMenu ysMenu = new YsMenu().setId(menuId).queryFirst();
		Long gridId = ysMenu.getGridId();
		if (gridId == null) {
			return R.error("尚未配置子列表");
		}
		
		Integer styleType = 1;
		String name = "按钮名称待完善";
		String event = "按钮事件待完善";
		String icon = "按钮图标待完善";
		String nameShiroAuth = "xxxx";
		Integer sortNo = 999;
		String remark = null;
		return ysGridButtonService.add(gridId, styleType, name, event, icon, nameShiroAuth, sortNo, remark);
	}
	
	/**
	 * 编辑
	 */
	@PostMapping("/edit")
    @ResponseBody
	public R edit(Model model, HttpServletRequest request, HttpServletResponse response,
			@RequestParam Map<String, Object> params){
		
		return ysGridButtonService.edit(params);
	}
	
	/*
	 * 彻底删除
	 */
	@PostMapping("/deleteTrue")
	@ResponseBody
	public R deleteTrue(HttpServletRequest request, HttpServletResponse response,
			Long id) {
		return ysGridButtonService.deleteTrue(id);
	}
	
}
